$(function () {
	function pushInfo(){
		$.post('/UserInfo/findUserInfo.do', {},function(jsonData){
		
		//var infoJson = {"id":"0","roles":[],"username":"admin","password":"123456","code":null,"lastLoginOutTime":null,"realname":"管理员","dept":null,"jobs":null,"sex":null,"phone":null,"email":null,"lastLoginTime":null}
			$.each(jsonData,function(item, value){
			var itemObj = 'input[name="'+item+'"]';
			if($('.myinfoForm').find(itemObj)){$('.myinfoForm').find(itemObj).val(value);}
			});
		});
	}
	
	function infoTips(tips, cls){		
		$( "#dialog-confirm" ).html(tips).removeClass('hide').dialog({
			resizable: false,
			modal: true,
			title: "提示",
			title_html: true,
			buttons: [
				{
					html: "<i class='icon-trash'></i>&nbsp; 关闭",
					"class" : "btn btn-xs",
					click: function() {
						$( this ).dialog( "close" );
					}
				}
				,
				{
					html: "<i class='icon-remove'></i>&nbsp; 确定",
					"class" : "btn btn-xs "+cls,
					click: function() {
						$( this ).dialog( "close" );
					}
				}
			]
		});
	}
	$('.resetInfo').bind('click', function(){
		pushInfo();
	})
	pushInfo();
	$.validator.setDefaults({
		submitHandler: function() {
		    $.post('/UserInfo/updateUserInfo.do', $('.myinfoForm').serialize(), function(data){
		    	if(!data){
		    		RM.tips("对不起，服务器繁忙，请稍候重试", "btn-danger");
				}else{
					RM.tips("修改成功", "btn-primary");
				}
			});
		}
	});
	$('.myinfoForm').validate({
		rules: {
		  username: {
			required: true,
			minlength: 2
		  },
//		  password: {
//			required: true,
//			minlength: 5
//		  },
		  email: {
			email: true
		  }
		},
		messages: {
			username: {
				required: "请输入姓名",
				minlength: "用户名必需由两个字母组成"
			},
//			password: {
//				required: "请输入密码",
//				minlength: "密码长度不能小于 5 个字母"
//			},
			email: "请输入一个正确的邮箱"
		}
	})
});